package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
public class SignServiceImpl implements ISignService {

    @Autowired
    private SignMapper signMapper;

    @Override
    public void save(Sign sign) throws Exception {
        //获取当前用户
        User user=(User) UserContext.getCurrentUser();

        //格式化日期
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        String str=sd.format(new Date());
        Date date=sd.parse(str);

        //查询今天是否有签到
        Sign sign0=signMapper.selectByDate(user.getId(),date);//今天的签到记录

        //查询昨天是否有签到
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        //获取前一天
        calendar.add(Calendar.DATE,-1);
        Date yesterday = calendar.getTime();
        String str1=sd.format(yesterday);
        Date date1=sd.parse(str1);
        Sign sign1=signMapper.selectByDate(user.getId(),date1);//昨天签到记录

        //当天未签到
        if (sign0==null){

            //查看该用户是否有签到历史记录
            Sign sign2 = signMapper.selectByUserId(user.getId());//该用户是否有签记录,有记录,执行更新操作;没记录执行保存操作

            //有记录,表示签到过
            if(sign2!=null){

                //昨天是否有签到
                //昨天没签到,不是连续签到
                if (sign1==null){
                    //设置连续签到天数为1
                    sign.setDays(1);
                //昨天已签到
                }else{
                    //在昨天的签到天数上加1
                    sign.setDays(sign1.getDays()+1);
                    sign.setPoints(sign1.getPoints()+1);
                    sign.setUser(sign1.getUser());
                    //如果连续签到两天以上,积分+2
                    if (sign1.getDays()>=2){
                        sign.setPoints(sign1.getPoints()+2);
                        //如果连续签到三天以上,积分翻倍
                        if (sign1.getDays() % 3 == 0){
                            sign.setPoints(sign1.getPoints()*2);
                        }
                    }
                }
                sign.setSign_time(date);
                signMapper.update(sign);

            //没有记录,表示还没有签到过
            }else{
                sign.setPoints(1);
                sign.setSign_time(date);
                sign.setDays(1);
                sign.setUser(user);
                signMapper.insert(sign);
            }

        //当天已签到
        }else{
            throw new RuntimeException("今日已签到");
        }
    }

    //获取当前用户的积分
    @Override
    public Sign getPointByUserId() {
        User user = UserContext.getCurrentUser();
        return signMapper.selectByUserId(user.getId());
    }

    /**
     * 更新用户积分的操作
     * @param sign
     */
    @Override
    public void updatePoints(Sign sign) {
       signMapper.updatePoints(sign);
    }

    /**
     * 通过当前的登录用户判断,今日是否已经签到
     */
    @Override
    public void get()throws Exception  {
        //格式化日期
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        String str=sd.format(new Date());
        Date date=sd.parse(str);
        User user=(User) UserContext.getCurrentUser();
        Sign sign=signMapper.selectByDate(user.getId(),date);//今天的签到记录
        if (sign != null){
            throw new RuntimeException("今日已签到");
        }
    }
}
